package com.hzhr.iot.collect.receiver;

import com.hzhr.iot.collect.model.CommandResponseModel;
import com.hzhr.iot.collect.model.CommonResponseModel;
import com.hzhr.iot.collect.model.ReceiveCommandModel;
import com.hzhr.iot.collect.protocol.flowController.FlowController;
import com.hzhr.iot.collect.util.StrUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "command-receiver", description = "命令接收器")
@RestController
public class CommandReceiver {
    @Autowired
    FlowController flowController1;
    @Autowired
    FlowController flowController2;
    @Autowired
    FlowController flowController3;

    private static Logger logger = LoggerFactory.getLogger(CommandReceiver.class);

    @RequestMapping(value = "/commands/receive", method = RequestMethod.POST)
    public CommandResponseModel receiveCommand(
            @RequestBody @ApiParam(name="receiveCommandDto(发送命令请求对象)", required=true) ReceiveCommandModel requestBody)
            throws Exception {
        logger.info("/commands/receive   body:" + HzhrJsonUtil.object2json(requestBody));

        Date receiveTime = new Date();//服务器接收时间

        String message = null;

        CommandResponseModel result = new CommandResponseModel();

        if (requestBody.getService() != null) {//华为iot平台数据指令
            message = requestBody.getService().getData().getData();

            if (message.startsWith("68") && message.endsWith("16")) {//NB协议
                result = flowController1.run(message, 1, requestBody.getDeviceId());
            } else if (message.startsWith("68") && message.endsWith("17")) {//压力监测仪协议
                result = flowController3.run(message, 3, requestBody.getDeviceId());
            }
        } else if (requestBody.getSocketServiceData() != null) {//socket-service指令
            message = requestBody.getSocketServiceData().get("data");

            String deviceId = StrUtil.removeZeroForLeft(message.substring(14, 28));

            if (message.startsWith("68") && message.endsWith("16")) {//重燃前置机协议
                result = flowController2.run(message, 2, deviceId);
            }
            if (message.startsWith("69") && message.endsWith("16")) {//合众慧燃集中器协议
                result = flowController2.run(message, 4, deviceId);
            }
        }

        return result;
    }

    @RequestMapping(value = "/commands/receive", method = RequestMethod.GET)
    public CommonResponseModel receiveCommand1()
            throws Exception {
        System.out.println("ok");
        return new CommonResponseModel();
    }
}



